package com.skplanet.oneplusonewindow.moim;

import java.util.HashMap;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainActivity extends Activity {

	static int scheight = 100;
	SocketChat chat;
	Activity activity;
	int height;
	int width;
	HashMap<String , StringBuilder> contentHash = new HashMap<String , StringBuilder>();
	HashMap<String , TextView> textViewHash = new HashMap<String , TextView>();
	HashMap<String , ScrollView> scViewHash = new HashMap<String , ScrollView>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_main);
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		height = displaymetrics.heightPixels;
		width = displaymetrics.widthPixels;
		
		activity = this;
		Thread chatThread = new Thread(new Runnable(){
		
			@Override
			public void run() {
				chat = new SocketChat(activity, user.getInstance().getName());
			}
			
		});
		chatThread.start();
		
		Button closebutton = (Button)findViewById(R.id.close);
		closebutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MainActivity.this.finish();
			}
		});
		
		final Button openbutton = (Button)findViewById(R.id.open);
		openbutton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(MainActivity.scheight == 200) {
					MainActivity.scheight = 400;
					openbutton.setText("닫기");
				}
				else { 
					MainActivity.scheight = 200;
					openbutton.setText("펼치기");
				}
				
				ScrollView infoview = (ScrollView)findViewById(R.id.scrollView1);
				RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)infoview.getLayoutParams();
				params.height = scheight;
				infoview.setLayoutParams(params);
			}
		});
		
		Button button = (Button)findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
		
			public void onClick(View v) {
				EditText edit = (EditText)findViewById(R.id.editText1);
				chat.send(edit.getText().toString());
				edit.setText("");
			}
		});
		contentHash.put("info", new StringBuilder());
		textViewHash.put("info", (TextView)findViewById(R.id.info));
		scViewHash.put("info", (ScrollView)findViewById(R.id.scrollView1));
	}
	public void newOne(String name) {
		ViewGroup layout = (ViewGroup) findViewById(R.id.linear1);
		ScrollView sv = new ScrollView(this);
		sv.setPadding(5, 5, 5, 5);
		layout.addView(sv);
		TextView tv = new TextView(this);
		tv.setLayoutParams(new LayoutParams(width/5, LayoutParams.WRAP_CONTENT));
		tv.setText(name + "]");
		tv.setBackgroundResource(R.drawable.back);
		tv.setPadding(10, 3, 3, 1);
		tv.setTextColor(Color.BLACK);
		sv.addView(tv);
		scViewHash.put(name, sv);
		textViewHash.put(name, tv);
		contentHash.put(name, new StringBuilder(name + "]"));
	}
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			
			if(msg.what == 0)
			{
				String name;
				StringBuilder cont = null;
				StringBuilder cont2 = null;
				if(chat.getContent().indexOf("#") == 0) {
					name = "info";
					cont2 = contentHash.get(name);
					cont2.append("\n" + chat.getContent());
				}
				else {
					String content[] = chat.getContent().toString().split("]");
					int co = content.length;
					name = content[0];
					if(textViewHash.get(name) == null)
						newOne(name);
				
					cont = contentHash.get(name);
					cont2 = contentHash.get("info");
					cont = new StringBuilder(name + "]\n\n");
					cont2.append("\n" + name + "] : ");
					for(int i = 0 ; i < co-1 ; i ++) {
						cont.append(content[i+1]);
						cont2.append(content[i+1]);
					}
				}
				if(name != "info" && cont != null) {
					TextView t = textViewHash.get(name);
					t.setText(cont);
					final ScrollView scrollView = scViewHash.get(name);
					scrollView.post(new Runnable() {   
						   public void run() {
							   scrollView.fullScroll(View.FOCUS_DOWN);
							   }
						 });
				}
				TextView t2 = textViewHash.get("info");
				t2.setText(cont2);

				final ScrollView scrollView = scViewHash.get("info");
				scrollView.post(new Runnable() {   
					   public void run() {
						   scrollView.fullScroll(View.FOCUS_DOWN);
						   }
					 });
			}
		}
		
	};
	

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		chat.close();
		super.onDestroy();
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	

}
